<template>
  <div class="app-container">
    <el-form :model="searchForm" ref="searchFormRef" size="small" inline v-show="showSearch">
      <el-form-item label="字典名称" prop="where.type">
        <el-select v-model="searchForm.where.type" class="w-140px" disabled>
          <el-option v-for="item in typeOptions" :key="item.type" :label="item.name" :value="item.type"/>
        </el-select>
      </el-form-item>
      <el-form-item label="字典标签" prop="like.label">
        <el-input v-model="searchForm.like.label" placeholder="请输入字典标签" clearable />
      </el-form-item>
      <el-form-item label="状态" prop="where.status">
        <el-select v-model="searchForm.where.status" placeholder="数据状态" clearable class="w-120px">
          <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="mb-8px reset">
      <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd" v-hasPerm="['system:dict-data:add']">新增</el-button>
      <el-button type="success" plain icon="el-icon-edit" @click="handleEdit" v-hasPerm="['system:dict-data:edit']" :disabled="ids.length !== 1">修改</el-button>
      <el-button type="danger" plain icon="el-icon-delete" @click="handleDelete" v-hasPerm="['system:dict-data:delete']" :disabled="!ids.length">删除</el-button>
      <el-button type="info" plain icon="el-icon-close" @click="closeTab">关闭</el-button>
      <right-toolbar :showSearch.sync="showSearch" @refresh="getList" />
    </div>

    <el-table v-loading="loading" :data="listData" @selection-change="handleSelection">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id" width="55"/>
      <el-table-column label="字典标签" align="center" prop="label">
        <template slot-scope="{ row }">
          <span v-if="(!row.list_class || row.list_class === 'default')" :class="['dict-default',row.css_class]">{{ row.label }}</span>
          <el-tag v-else :type="row.list_class" :class="row.css_class">{{ row.label }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="字典键值" align="center" prop="value"/>
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template v-slot="{ row }">
          <dict-tag :options="dict.type.sys_normal_disable" :value="row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="字典排序" align="center" prop="list_sort" width="80"/>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip/>
      <el-table-column label="默认" align="center" prop="is_default" width="80">
        <template v-slot="{ row }">
          <dict-tag :options="dict.type.sys_yes_no" :value="row.is_default"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="create_time" width="140">
        <template v-slot="{ row }">
          <span>{{ parseTime(row.create_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template v-slot="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(row)" v-hasPerm="['system:dict-data:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row)" v-hasPerm="['system:dict-data:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-if="total>0" :total="total" :page.sync="searchForm.pageNum" :limit.sync="searchForm.pageSize" @pagination="getList"/>
    <dict-data-form v-model="formShow" :module-name="moduleName" :row="formData" @submit="getList" v-if="formShow"/>
  </div>
</template>

<script>
import { listDataApi, delDataApi } from '@/api/system/dict/data'
import { infoTypeApi } from '@/api/system/dict/type'
import DictDataForm from './dataForm'

export default {
  name: 'DictData',
  dicts: ['sys_normal_disable', 'sys_yes_no'],
  components: { DictDataForm },
  data() {
    return {
      moduleName: '字典数据',
      dictId: '',
      dictType: '',
      typeOptions: [],
      loading: true,
      ids: [],
      showSearch: true,
      total: 0,
      listData: [],
      formData: null,
      formShow: false,
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        where: { type: undefined, status: undefined },
        like: { label: undefined }
      }
    }
  },
  created() {
    this.dictId = this.$route.params?.dictId || ''
    this.loading = true
    this.getTypeData()
  },
  methods: {
    getList() {
      this.loading = true
      listDataApi(this.searchForm).then(res => {
        this.listData = res.list
        this.total = res.total
        this.loading = false
      })
    },
    getTypeData() {
      infoTypeApi(this.dictId).then(res => {
        this.typeOptions = [{ type: res.info.type, name: res.info.name || '未知类型' }]
        this.dictType = res.info.type
        this.reset()
      })
    },
    closeTab() {
      this.$tab.closeOpenPage({ path: '/system/dict' })
    },
    search() {
      this.searchForm.pageNum = 1
      this.getList()
    },
    reset() {
      this.resetForm('searchFormRef')
      this.searchForm.where.type = this.dictType
      this.search()
    },
    handleAdd() {
      this.formData = { type: this.dictType }
      this.formShow = true
    },
    handleEdit(row) {
      const editId = row?.id || (this.ids.length? this.ids[0] : '')
      if(!editId) return this.$modal.msgError('参数缺失')
      this.formData = { id: editId }
      this.formShow = true
    },
    handleSelection(selection) {
      this.ids = selection.map(item => item.id)
    },
    handleDelete(row) {
      const delIds = row.id || this.ids.join(',')
      if (!delIds) return this.$modal.msgWarning('未选择数据')
      this.$modal.confirm('是否确认删除字典编号为"' + delIds + '"的数据项？').then(() => {
        return delDataApi(delIds)
      }).then((res) => {
        this.$store.dispatch('dict/removeDict', this.searchForm.dictType)
        this.$modal.msgSuccess(res.msg || '删除成功')
        this.getList()
      }).catch(() => {})
    },
  }
}
</script>
